package optimizers.ganeat.fcd;

import java.awt.GridLayout;
import java.util.Map;

import javax.swing.JPanel;

import optimizers.commons.RunConfig;
import optimizers.ganeat.amplify.AmplifyIndividual;
import util.MyUtil;
import visualization.PlotExpData;
import model.Input;
import model.OligoSystemGeneral;

public class IndividualFCD extends AmplifyIndividual {

	public IndividualFCD(Map<String, Integer> innovations,
			Map<String, String> nodeNames, int geneIndex, int indivIndex,
			RunConfig config) {
		super(innovations, nodeNames, geneIndex, indivIndex, config);
		// TODO Auto-generated constructor stub
	}

	public IndividualFCD(Map<String, Integer> innovations,
			Map<String, String> nodeNames, int geneIndex, int indivIndex,
			RunConfig config, String string) {
		super(innovations, nodeNames, geneIndex, indivIndex, config, string);
		// TODO Auto-generated constructor stub
	}

	@Override
	public JPanel displayTimeSeriesWithButton() {
		JPanel panel = new JPanel();
		GridLayout grid = new GridLayout(2, 1);
		panel.setLayout(grid);
		grid.setColumns(1);

		OligoSystemGeneral oligoModel = getOligoModel();

		double[] inputsAmmount = MyUtil.getInputsForFCD(ConstantsFCD.minTest,
				ConstantsFCD.maxTest, ConstantsFCD.nTests);
		Input[] inputs = new Input[inputsAmmount.length];
		for (int i = 0; i < inputsAmmount.length; i++) {
			inputs[i] = new Input(1, inputsAmmount[i], Integer.MAX_VALUE);
		}

		boolean[] sequenceProtected = new boolean[oligoModel.nSimpleSequences];
		sequenceProtected[0] = true;

		for (int i = 1; i < oligoModel.nSimpleSequences; i++) {
			sequenceProtected[i] = false;
		}
		oligoModel.sequenceProtected = sequenceProtected;
		double[] reporterConcentration = new double[oligoModel.nSimpleSequences];
		for (int i = 0; i < oligoModel.nSimpleSequences; i++) {
			reporterConcentration[i] = 0;
		}
		reporterConcentration[1] = ConstantsFCD.reporterConcentration;
		oligoModel.reporterConcentration = reporterConcentration;
		oligoModel.seqConcentration[0][0] = ConstantsFCD.minTest;
		double[][] data = oligoModel.getTimeSeriesWithInputComplexModel(inputs);
		String[] seqNames = new String[data.length];
		String outputName = oligoModel.intToName.get(1);
		for (int j = 0; j < data.length; j++) {
			if (j == data.length - 1) {
				seqNames[j] = outputName + "(r)";
			} else if (j >= 2) {
				seqNames[j] = oligoModel.intToName.get(j - 1);
			} else {
				seqNames[j] = j % 2 == 0 ? oligoModel.intToName.get(j / 2)
						+ "'" : oligoModel.intToName.get(j / 2);
			}
			if (seqNames[j] == null)
				seqNames[j] = String.valueOf(j);
		}
		final PlotExpData demo = new PlotExpData("Generation "
				+ this.generationIndex + " Index " + this.individualIndex,
				data, seqNames, "Time", "Expression");
		panel.add(demo);
		return panel;
	}

	private static final long serialVersionUID = 5215001072434783056L;

}
